
package com.market.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.market.bean.Customer;
import com.market.bean.Subknowledge;
import com.market.bean.Subnews;
import com.market.bean.Subproduct;
import com.market.web.form.CusSearchForm;

public class CustomerServiceImpl extends BaseServiceImpl implements ICustomerService
{
    /*
     * (non-Javadoc)
     * 
     * @see com.market.service.ICustomerService#saveOrUpdate(com.market.bean.Customer)
     */
    public Customer FindById(long id)
    {

        Customer cus = getCustomerDAO().findById(id);
        if (cus != null)
        {
            DetachedCriteria d1 = DetachedCriteria.forClass(Subknowledge.class);
            d1.add(Restrictions.eq("cusid", new Long(cus.getId())));
            cus.setSubknowledges(new HashSet(getBaseDAO().findByDetchedCriteria(d1, 0, 0)));
            DetachedCriteria d2 = DetachedCriteria.forClass(Subnews.class);
            d2.add(Restrictions.eq("cusid", new Long(cus.getId())));
            cus.setSubnewses(new HashSet(getBaseDAO().findByDetchedCriteria(d2, 0, 0)));
        }
        return cus;
    }

    public void removeSubMsg(Customer cus)
    {
        DetachedCriteria dknow = DetachedCriteria.forClass(Subknowledge.class);
        dknow.add(Restrictions.eq("cusid", new Long(cus.getId())));
        deleteByDetachedCriteria(dknow);
        System.out.println("delete sub msg");
        DetachedCriteria dnews = DetachedCriteria.forClass(Subnews.class);
        dnews.add(Restrictions.eq("cusid", new Long(cus.getId())));
        deleteByDetachedCriteria(dnews);
    }

    public int getCountByForm(CusSearchForm csf)
    {
        // TODO Auto-generated method stub
        DetachedCriteria detachCri = getDetachedCriteriaByForm(csf);
        return getCustomerDAO().getCountByDetachedCriteria(detachCri);
    }

    public List findByForm(CusSearchForm csf, int start, int max)
    {
        // TODO Auto-generated method stub
        DetachedCriteria detachCri = getDetachedCriteriaByForm(csf);
        return getCustomerDAO().findByDetchedCriteria(detachCri, start, max);
    }

    public List findByMessage(String type, String category)
    {
        // TODO Auto-generated method stub
        DetachedCriteria detachCri = null;
        if ("news".equals(type))
        {
            detachCri = DetachedCriteria.forClass(Subnews.class);
            detachCri.add(Restrictions.eq("category", category));
            List l = getBaseDAO().findByDetchedCriteria(detachCri, 0, 0);
            List cusList = new ArrayList();
            for (int i = 0; i < l.size(); i++)
            {
                Subnews subn = (Subnews)l.get(i);
                Customer cus = getCustomerDAO().findById(subn.getCusid());
                if (cus != null)
                {
                    cusList.add(cus);
                }
            }
            return cusList;
        }
        if ("know".equals(type))
        {
            detachCri = DetachedCriteria.forClass(Subknowledge.class);
            detachCri.add(Restrictions.eq("category", category));
            List l = getBaseDAO().findByDetchedCriteria(detachCri, 0, 0);
            List cusList = new ArrayList();
            for (int i = 0; i < l.size(); i++)
            {
                Subknowledge subk = (Subknowledge)l.get(i);
                Customer cus = getCustomerDAO().findById(subk.getCusid());
                if (cus != null)
                {
                    cusList.add(cus);
                }
            }
            return cusList;
        }
        return null;
    }

    public List findByProductId(Long[] id)
    {
        // TODO Auto-generated method stub
        DetachedCriteria detachCri = DetachedCriteria.forClass(Subproduct.class);
        detachCri.add(Restrictions.in("productid", id));
        // detachCri.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
        List list = getSubproductDAO().findByDetchedCriteria(detachCri, 0, 0);
        Set cusList = new HashSet();
        for (int i = 0; i < list.size(); i++)
        {
            Subproduct subp = (Subproduct)list.get(i);
            Customer cus = getCustomerDAO().findById(subp.getCusid());
            if (cus != null)
            {
                cusList.add(cus);
            }
        }
        return new ArrayList(cusList);
    }

    private DetachedCriteria getDetachedCriteriaByForm(CusSearchForm csf)
    {
        DetachedCriteria detachCri = DetachedCriteria.forClass(Customer.class);
        if ("true".equals(csf.getSex()) || "false".equals(csf.getSex()))
        {
            detachCri.add(Restrictions.eq("sex", Boolean.valueOf(csf.getSex())));
        }
        if (!"".equals(csf.getAddress()))
        {
            detachCri.add(Restrictions.like("address", csf.getAddress(), MatchMode.ANYWHERE));
        }
        if (!"".equals(csf.getName()))
        {
            detachCri.add(Restrictions.like("name", csf.getName(), MatchMode.ANYWHERE));
        }
        if (!"".equals(csf.getId()))
        {
            long id = 0;
            try
            {
                id = Long.parseLong(csf.getId());
            }
            catch (Exception e)
            {
                // TODO: handle exception
            }
            if (id != 0)
            {
                detachCri.add(Restrictions.idEq(new Long(id)));
            }
        }
        if ((-1) != csf.getLevel())
        {
            detachCri.add(Restrictions.eq("level", new Integer(csf.getLevel())));
        }
        return detachCri;
    }

}
